home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12974 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.2 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: beginner question
  5. Date: 3 Apr 1996 17:59:19 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4jue9n$gm1@sparcserver.lrz-muenchen.de>
  9. References: <4jc3sr$1ggu@uvaix3e1.comp.UVic.CA> <4jdo7l$de2@sparcserver.lrz-muenchen.de> <315AFED2.7466@willows.com> <315F525A.4A1CD496@alcyone.com> <31625C30.12AA@sooner.net>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. Eddie Bush <edwbush@sooner.net> writes:
  13.  
  14. >Erik Max Francis wrote:
  15. >> 
  16. >> Tarang Deshpande wrote:
  17. >> 
  18. >> > So then what does the following mean:
  19. >> >
  20. >> > struct _FOO
  21. >> > {
  22. >> >         int     bar;
  23. >> > } FOO;
  24. >> >
  25. >> > struct _FOO s1;
  26. >> > FOO         s2;
  27. >> >
  28. >> > Why?
  29. >> 
  30. >> This is a compiler error.
  31.  
  32. >I'm sorry.  I don't see an error -- just two different variables of the same type that happen to 
  33. >be declared using two different (and correct) methods.
  34.  
  35. The first is, as Erik states correctly, a valid definition for s1, whereas
  36. the second is similar to
  37.  
  38.   int a;
  39.   a b;
  40.  
  41. Without an operator between "a" and "b", this is not valid in C. A compiler
  42. must issue a diagnostic message for a program that contains that statement.
  43.  
  44. >> 
  45. >>     struct _FOO { int bar; } FOO;
  46. >> 
  47. >> declares a structure called struct _FOO and an instance of that structure FOO.
  48. >> The further declaration
  49.  
  50. >The initial declaration of the structure doesn't make available FOO for
  51. >manipulation.  FOO is a 
  52. >type.  ...isn't it? 
  53.  
  54. No, it's not! "FOO" is a variable, and "struct _FOO" is it's type.
  55.  
  56. >I always understood that 'struct _FOO' is the 'tag name' (the name of the 
  57. >structure', and that FOO would be a type which you have defined (by the typedef struct _FOO) to 
  58. >be the structure _FOO.
  59.  
  60. There _is_ no "typedef" in "struct _FOO { int bar; } FOO;", nor was there
  61. a "typedef" in the original article. Erik's diagnosis seems to be correct.
  62. The original poster "missed" the "typedef".  
  63.  
  64. >Also, from what I understand, you would do it like this:
  65.  
  66. >typedef struct {
  67. >    int num;
  68. >} FOOBAR;
  69.  
  70. Type "FOOBAR" is an anonymous struct.
  71.  
  72. >This would be if you intended to use the type statically.  Or, you could:
  73.  
  74. >typedef struct FOO {
  75. >    int num;
  76. >} *BAR;
  77.  
  78. Type "BAR" is a pointer to "struct FOO".
  79.  
  80. >if you happened to want to allocate the variable dynamically.
  81.  
  82. The main question is whether you want to declare a pointer type or
  83. not. "BAR" can well be used without any dynamic memory allocation.
  84.  
  85.    struct FOO x;
  86.    BAR y = &x;
  87.  
  88. >Notice, that I left the 'tag name' off of the variable which I intend to
  89. >use statically.  That is 
  90. >because it is not needed -- you only need it to allocate memory dynamically:
  91.  
  92. >BAR new_bar;
  93.  
  94. >new_bar = (BAR) malloc (sizeof (struct FOO));
  95.  
  96. Have you tried
  97.  
  98.    new_bar = malloc(sizeof(*new_bar));
  99.  
  100. The "struct tag" is needed for, e.g., recursive data types, but _not_
  101. for memory allocation.
  102.  
  103. >Maybe this helps (and doesn't confuse) someone.  I felt very confused by
  104. >the string of preceding 
  105. >messages -- maybe it's the time of day.  Who knows...
  106.  
  107. Get some sleep. Read the "string of preceding messages" again. Esp. the
  108. message from Erik.
  109.  
  110. Kurt
  111. --
  112. | Kurt Watzka                             Phone : +49-89-2180-6254
  113. | watzka@stat.uni-muenchen.de
  114.